package com.paishen.peiniwan.module.profile;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.paishen.peiniwan.R;

public class ProfileTagEditDialog extends Dialog {

    public ProfileTagEditDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Params {
        public Context context;
        public DialogInterface.OnMultiChoiceClickListener callback;
        public DialogInterface.OnClickListener cancelListener, confirmListener;
        public int confirmTextId, cancelTextId;
        public int title;
        public TagAdapter adapter;
        public CharSequence[] items;
        public boolean[] checkedItems;
    }

    public static class Builder {
        private final Params p;

        private boolean canCancel = true;
        private boolean shadow = true;

        public Builder(Context context) {
            p = new Params();
            p.context = context;
        }

        public Builder setTitle(int title) {
            this.p.title = title;
            return this;
        }

        public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
                                           final DialogInterface.OnMultiChoiceClickListener listener) {
            this.p.items = items;
            this.p.checkedItems = checkedItems;
            this.p.callback = listener;
            return this;
        }

        public Builder setPositiveButton(int textId, final DialogInterface.OnClickListener listener) {
            this.p.confirmTextId = textId;
            this.p.confirmListener = listener;
            return this;
        }

        public Builder setNegativeButton(int textId, final DialogInterface.OnClickListener listener) {
            this.p.cancelTextId = textId;
            this.p.cancelListener = listener;
            return this;
        }

        public ProfileTagEditDialog create() {
            final ProfileTagEditDialog dialog = new ProfileTagEditDialog(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
//            Window window = dialog.getWindow();
//            window.setWindowAnimations(R.style.Animation_Bottom_Rising);
//
//            window.getDecorView().setPadding(0, 0, 0, 0);
//            WindowManager.LayoutParams lp = window.getAttributes();
//            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
//            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//            window.setAttributes(lp);
//            window.setGravity(Gravity.BOTTOM);

            View view = LayoutInflater.from(p.context).inflate(R.layout.dialog_profile_info_edit_tag, null);

            RecyclerView viewRecycler = (android.support.v7.widget.RecyclerView) view.findViewById(R.id.view_recycler);
            viewRecycler.setLayoutManager(new GridLayoutManager(p.context, 2));
            viewRecycler.setAdapter(p.adapter = new TagAdapter(dialog, p.items, p.checkedItems, p.callback));
            viewRecycler.setItemAnimator(new DefaultItemAnimator());

            TextView txTitle = (TextView) view.findViewById(R.id.tx_title);
            if (p.title > 0) txTitle.setText(p.title);

            Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
            if (p.cancelTextId > 0) btnCancel.setText(p.cancelTextId);
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (p.cancelListener != null) p.cancelListener.onClick(dialog, v.getId());
                }
            });

            Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
            if (p.cancelTextId > 0) btnConfirm.setText(p.confirmTextId);
            btnConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (p.confirmListener != null) p.confirmListener.onClick(dialog, v.getId());
                }
            });

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);
            return dialog;
        }
    }

    static class TagAdapter extends RecyclerView.Adapter<TagAdapter.ViewHolder> {
        public CharSequence[] items;
        public boolean[] checkedItems;
        public DialogInterface.OnMultiChoiceClickListener callback;
        public DialogInterface dialogInterface;

        TagAdapter(DialogInterface dialogInterface, CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) {
            this.dialogInterface = dialogInterface;
            this.items = items;
            this.checkedItems = checkedItems;
            this.callback = listener;
        }

        @Override
        public int getItemCount() {
            return items.length;
        }

        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            public ViewHolder(View view) {
                super(view);
                view.setOnClickListener(this);
            }

            private TextView txContent;
            private ImageView imgState;
            private int position;

            @Override
            public void onClick(View v) {
                final boolean curr = checkedItems[position];
                checkedItems[position] = !curr;
                callback.onClick(dialogInterface, position, checkedItems[position]);

                imgState.setImageResource(checkedItems[position] ? R.mipmap.checkbox_checked : R.mipmap.checkbox_uncheck);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View viewItem = LayoutInflater.from(parent.getContext()).inflate(com.paishen.peiniwan.R.layout.recycler_profile_edit_tag, parent, false);
            ViewHolder holder = new ViewHolder(viewItem);
            holder.txContent = (TextView) viewItem.findViewById(R.id.tx_content);
            holder.imgState = (ImageView) viewItem.findViewById(R.id.img_state);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.imgState.setImageResource(checkedItems[position] ? R.mipmap.checkbox_checked : R.mipmap.checkbox_uncheck);
            holder.txContent.setText(items[position]);
            holder.position = position;
        }
    }
}
